#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/12/23 下午6:26
# @Author  : fugang_le
# @Software: PyCharm


import requests
import json

headers = {'Content-Type': 'application/json'}

server_url = "http://10.1.19.114:8122"
server_url = "http://192.168.0.197:8122"
server_url = "http://10.1.12.181:8122"
# server_url = "http://10.1.12.84:8122"


def post_classify(texts, topk=1):
    url = server_url + '/material_identification/classify'
    data = {"data": str(texts), 'topk':topk}
    result = requests.post(url=url, data=data).text
    return json.loads(result)


def post_identify(classification, material_name, material_sepc,price_conditions, price_type, topk=1):
    url = server_url + '/material_identification/identify'
    data_ = [{"material_name": material_name, "material_sepc": material_sepc, 'price_conditions':price_conditions, 'classification':classification, 'price_type':price_type}]
    data = {"data": str(data_),  'topk':topk}
    result = requests.post(url=url, data=data).text
    return json.loads(result)


def post_classify_and_indentify(material_name, material_sepc, price_conditions, price_type, topk=1):
    url = server_url + '/material_identification/classify_and_indentify'
    data_ = [{"material_name": material_name, "material_sepc": material_sepc, 'price_conditions': price_conditions, 'price_type':price_type}]
    data = {"data": str(data_),  'topk':topk}
    result = requests.post(url=url, data=data).text
    return json.loads(result)


if __name__ == '__main__':
    import time
    st = time.time()
    price_conditions = {
        'price_condition_one': {'fir_area': '', 'seco_area': '', 'third_area': '', 'particular_year': '',
                                'time_version': ''},
        'price_condition_two': {'fir_area': '', 'seco_area': '', 'third_area': '', 'particular_year': '',
                                'time_version': ''},
        'price_condition_three': {'fir_area': '', 'seco_area': '', 'third_area': '', 'particular_year': '',
                                  'time_version': ''},

    }

    price_type = '材料资源库'
    classification = ''
    material_name = '钢绞线'
    material_sepc = ''
    topk = 10
    result = post_classify_and_indentify(material_name, material_sepc, price_conditions, price_type, topk)
    result = post_identify(classification, material_name, material_sepc, price_conditions, price_type, topk)
    result = post_classify([{'material_name':material_name, 'material_sepc':material_sepc, "price_type":price_type}], topk=topk)
    for item in result['data']['result']:
        print(item)
    et = str(round((time.time() - st) * 1000, 2))
    print("get similrity spend time: ", et)
